using System.Data.SQLite;
using 人员管理4;

namespace 人员管理SQLLITE
{
    public partial class Form1 : Form
    {
        private static readonly string connectionString = "Data Source=Person.db;Version=3;"; // 内存中的数据库实例  

        private PersonUtils personUtils;
        private int selId;

        public Form1()
        {
            InitializeComponent();

            personUtils = new PersonUtils();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bool b = personUtils.CreatePersonTable();
            if (b)
            {
                label1.Text = "数据库Person.db创建成功";
            }

            List<Person> persons = personUtils.SelectAll();
            dgv1.DataSource = persons;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            bool b = personUtils.DeletePerson(selId);
            if (b)
            {
                List<Person> persons = personUtils.SelectAll();
                dgv1 .DataSource = persons;
            }
            
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;
            List<Person> persons = personUtils.FindPersons(name,age,sex,addr);
            dgv1.DataSource = persons;
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            bool b = personUtils.InsertPerson(name, age, sex, addr);
            if (b)
            {
                List<Person> persons = personUtils.SelectAll();
                dgv1.DataSource = persons;
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            bool b = personUtils.EditPerson(selId, name, age, sex, addr);
            if (b)
            {
                List<Person> persons = personUtils.SelectAll();
                dgv1.DataSource = persons;
            }
        }

        /// <summary>
        /// 选中一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            int count = dgv1.SelectedRows.Count;
            if (count > 1)
            {
                MessageBox.Show($"你选择了{count}条记录，只能选择一条");
            }
            else if (count == 1)
            {
                DataGridViewRow row = dgv1.SelectedRows[0];
                if (row != null)
                {
                    foreach (DataGridViewCell c in row.Cells)
                    {
                        int index = c.ColumnIndex;
                        object value = c.Value;
                        if (value != null)
                        {
                            if (index == 0)
                            {
                                selId = Convert.ToInt32(c.Value);
                            }
                            else if (index == 1)
                            {
                                tb_name.Text = c.Value.ToString();
                            }
                            else if (index == 2)
                            {
                                tb_age.Text = c.Value.ToString();
                            }
                            else if (index == 3)
                            {
                                cb_sex.Text = c.Value.ToString();
                            }
                            else if (index == 4)
                            {
                                tb_addr.Text = c.Value.ToString();
                            }
                        }
                    }
                }
            }
        }
    }
}
